#include "Cube.h"

Cube::Cube() : _scale(Vector3(0.f, 0.f, 0.f)), _eulerAngle(Vector3(0.f, 0.f, 0.f)), m_shaderColor("Shaders/couleurs.vert", "Shaders/couleurs.frag"), m_vertex(0), m_red(0), m_green(0), m_blue(0), _size(1.0f)
{

	m_shaderColor.initialiser();

	_position = new float[3];
	for(int i = 0; i < 3; ++i)
		_position[i] = 0.0f;

	init();
	
}

Cube::Cube(float* position, float size) : _scale(Vector3(0.f, 0.f, 0.f)), _eulerAngle(Vector3(0.f, 0.f, 0.f)), m_shaderColor("Shaders/couleurs.vert", "Shaders/couleurs.frag"), m_vertex(0), m_red(0), m_green(0), m_blue(0)
{
	_size = size;

	_position = new float[3];

	for(int i = 0; i < 3; ++i)
		_position[i] = position[i];

	m_shaderColor.initialiser();

	init();
}

Cube::~Cube()
{
	delete[] m_vertex;
	delete[] m_index;
	delete[] m_red;
	delete[] m_green;
	delete[] m_blue;
	delete[] _position;
}

void Cube::init()
{
	_scale = Vector3(1, 1, 1);
	_eulerAngle = Vector3(0, 0, 0);

	//Vertex
	float vertex[] = {-_size / 2.0f, -_size / 2.0f, -_size / 2.0f,   _size / 2.0f, -_size / 2.0f, -_size / 2.0f,   _size / 2.0f, _size / 2.0f, -_size / 2.0f,   -_size / 2.0f, _size / 2.0f, -_size / 2.0f,		
		-_size / 2.0f, -_size / 2.0f, _size / 2.0f,   _size / 2.0f, -_size / 2.0f, _size / 2.0f,   _size / 2.0f, _size / 2.0f, _size / 2.0f,    -_size / 2.0f, _size / 2.0f, _size / 2.0f};

	//Index Vertex
	unsigned int index[] = {0, 1, 2,   2, 3, 0,		//Face 1
		5, 1, 2,   2, 6, 5,		//Face 2
		0, 1, 5,   0, 4, 5,		//Face 3
		4, 5, 6,   6, 7, 4,		//Face 4
		4, 0, 7,   0, 3, 7,		//Face 5
		2, 3, 6,   3, 6, 7};	//Face 6	

	//Color
	float red[] = {1.0, 0.0, 0.0,    1.0, 0.0,0.0,   1.0, 0.0, 0.0,    1.0, 0.0,0.0,   1.0, 0.0, 0.0,    1.0, 0.0,0.0,   1.0, 0.0, 0.0,    1.0, 0.0,0.0};
	float green[] = {0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0,   0.0, 1.0, 0.0};
	float blue[] = {0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,};
	float gray[] = {0.5f, 0.5f, 0.5f};

	//Size Array
	unsigned int const sizeVertex(8 * 3);
	unsigned int const sizeIndex(6 * 2 * 3);
	unsigned int const sizeColor(8 * 3);


	//Allocation
	m_vertex = new float[sizeVertex];
	m_index = new unsigned int[sizeIndex];

	m_red = new float[sizeColor];
	m_green = new float[sizeColor];
	m_blue = new float[sizeColor];
	m_color = new float[sizeColor];


	//Vertex copy
	for(int i(0); i < sizeVertex; i++)
		m_vertex[i] = vertex[i];


	//Index copy
	for(int i(0); i < sizeIndex; i++)
		m_index[i] = index[i];


	//Color copy
	for(int i(0); i < sizeColor; i++)
	{
		m_color[i] = gray[0];
	}
}

void Cube::draw(Matrice &modelview, Matrice &projection)
{
	modelview.push();

	modelview.translate(_position[0],_position[1],_position[2]);
	modelview.rotate(_eulerAngle.x, 1, 0, 0);
	modelview.rotate(_eulerAngle.y, 0, 1, 0);
	modelview.rotate(_eulerAngle.z, 0, 0, 1);
	modelview.scale(_scale.x, _scale.y, _scale.z);

	//Shader & Vertex Init
	glUseProgram(m_shaderColor.getProgramID());
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	//Shader Values
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "projection"), 1, GL_TRUE, projection.getValues());
	glUniformMatrix4fv(glGetUniformLocation(m_shaderColor.getProgramID(), "modelview"), 1, GL_TRUE, modelview.getValues());

	//Face 1
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_color);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, m_index);

	/*
	//Face 2
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_color);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &m_index[6]);
		
	//Face 3
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_color);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &m_index[12]);
		
	//Face 4
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_color);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &m_index[18]);

	//Face 5
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_color);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &m_index[24]);

	//Face 6
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,0, m_color);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &m_index[30]);
	*/

	//Disable shader and vertex		
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(0);
	glUseProgram(0);

	modelview.pop();
}



